<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <!-- 头部工具组件 -->
        <pageTools>
          <template #before>
            <el-button type="primary" size="small" @click="addCategory">添加分类</el-button>
          </template>
          <template #after>
            <el-input v-model="searchWord" placeholder="请输入关键字" class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="search" />
            </el-input>
          </template>
        </pageTools>
      </template>
      <!-- 表格 -->
      <el-table
        :data="categoryData"
        border
        style="width: 100%"
      >
        <el-table-column prop="image" label="图片" width="220" align="center">
          <template v-slot="{row}" class="imgbox">
            <img
              v-errImg="require('@/assets/404_images/404_cloud.png')"
              :src="row.image"
              alt=""
              class="image"
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          align="center"
        />
        <el-table-column
          prop="desc"
          label="介绍"
          align="center"
        />
        <el-table-column label="操作" align="center" width="300">
          <template v-slot="{row}">
            <el-button type="primary" size="small" @click="editCategory(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="delCategory(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        :page-size="size"
        layout="prev, pager, next"
        :total="total"
        @current-change="currentChange"
      />
    </el-card>
    <!-- 添加分类/编辑分类 弹窗 -->
    <AddCategory ref="addCategory" :show-dialog.sync="visibleDIalog" @updateEvent="getCategoryList" />
  </div>
</template>

<script>
import { categoryList, categoryCount, deleteCategory } from '@/api/category'
import AddCategory from './components/addCategory.vue'
export default {
  components: { AddCategory },
  data() {
    return {
      categoryData: [], // 分类列表数据
      size: 2, // 每页显示多少条数据
      total: 0, // 分类列表总数
      page: 0, // 页码
      searchWord: '', // 搜索关键字
      visibleDIalog: false // 控制添加分类弹窗显示隐藏
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    // 获取分类列表数据
    async getCategoryList() {
      if (this.searchWord) {
        const res = await categoryList({
          _limit: this.size,
          _start: this.page,
          title_contains: this.searchWord
        })
        this.categoryData = res
        // 获取分类总数量
        const result = await categoryCount({
          _limit: this.size,
          _start: this.page,
          title_contains: this.searchWord
        })
        this.total = result
      } else {
        const res = await categoryList({
          _limit: this.size,
          _start: this.page
        })
        this.categoryData = res
        // 获取分类总数量
        const result = await categoryCount({
          _limit: this.size,
          _start: this.page
        })
        this.total = result
      }
    },
    // 分页功能
    currentChange(val) {
      this.page = (val - 1) * 2
      this.getCategoryList()
    },
    // 搜索功能
    search() {
      this.getCategoryList()
    },
    // 添加分类
    addCategory() {
      this.visibleDIalog = true
    },
    // 删除分类
    delCategory(id) {
      // 询问框
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除分类接口
        await deleteCategory(id)
        // 判断删除的数据是否为最后一条数据
        if (this.categoryData.length === 1 && this.page !== 0) {
          this.page -= 2
        }
        // 重新渲染页面
        this.getCategoryList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.log(err)
      })
    },
    // 编辑分类
    editCategory(id) {
      // 显示弹窗
      this.visibleDIalog = true
      // 调用子组件getCategoryDate方法实现数据回显
      this.$refs.addCategory.getCategoryDate(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-pagination{
    text-align: center;
    margin-top: 20px;
}
.image{
    width: 200px;
    height: 200px;
    object-fit: cover;
}
</style>
